package com.hiking.jwt.controller;


import com.hiking.jwt.domain.Result;
import com.hiking.jwt.domain.UserBean;
import com.hiking.jwt.service.UserService;
import com.hiking.jwt.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping(value = "/register")
    public Result<UserBean> register(UserBean userBean) {
        UserBean newUser = userService.register(userBean);
        if (newUser != null) {
            return ResultUtil.success(newUser);
        } else {
            return ResultUtil.error(1, "注册失败");

        }
    }

    @PostMapping(value = "/mobileLogin")
    public Result<UserBean> login(UserBean userBean) {
        UserBean newUser = userService.login(userBean);
        if (newUser != null) {
            return ResultUtil.success(newUser);
        } else {
            return ResultUtil.error(1, "登录失败");

        }
    }

    @GetMapping(value = "/allUsers")
    public Result<UserBean> findAll() {
        ArrayList<UserBean> datas = userService.findAll();
        if (datas != null) {
            return ResultUtil.success(datas);
        } else {
            return ResultUtil.error(1, "用户列表获取失败");

        }
    }

}
